package com.jf.docsify.modules.document;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.RandomUtils;

import com.jf.docsify.common.exception.DocsifyException;
import com.jf.docsify.common.utils.AssertUtil;
import com.jf.docsify.common.utils.DbTxUtil;
import com.jf.docsify.common.utils.FileUtil;
import com.jf.docsify.common.utils.GitServerUtil;
import com.jf.docsify.config.DocsifyConfig;
import com.jf.docsify.modules.model.Organization;
import com.jf.docsify.modules.model.Settings;
import com.jf.docsify.modules.orgz.OrganizationService;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;

/**
 * Description:
 *
 * @author chenwr
 * @version 1.0
 *
 * <pre>
 * 修改记录:
 * 修改后版本           修改人       修改日期         修改内容
 * 2022/7/25.1       chenwr    2022/7/25           Create
 * </pre>
 * @date 2022/7/25
 */
public class DocService {

    @Inject
    private OrganizationService organizationService;

    private Settings settingsDao = new Settings().dao();

    public Page<Settings> docList(String organizationId, String currentPage, String pageSize) {
        return settingsDao.dao().paginate(Integer.valueOf(currentPage), Integer.valueOf(pageSize), "select * ", "from doc_settings where doc_org_id = ?", organizationId);
    }

    public boolean saveSetting(Settings settings, String userName, String password, String gitBaseURL) {
        Integer id = settings.getId();
        String name = settings.getName();
        String enName = settings.getEnName();
        Integer docOrgId = settings.getDocOrgId();
        AssertUtil.hasText(name, "文档名称不能为空");
        AssertUtil.hasText(enName, "文档名称(英文)不能为空");
        AssertUtil.notNull(docOrgId, "组织不能为空");
        settings.setCreateTime(new Date().getTime());
        if (id != null) {
            return settings.update();
        }
        List<Settings> settingsDb = settingsDao.find("select * from doc_settings where doc_org_id = ? and en_name = ?", docOrgId, enName);
        AssertUtil.isEmpty(settingsDb, "存在相同的文档名称(英文)，请重新设置");
        Organization org = organizationService.findById(String.valueOf(docOrgId));
        String path = org.getPath();
        settings.setDocOrgPath(path);
        settings.setBsId(RandomUtils.nextInt());
        return DbTxUtil.tx(() -> {
            GitServerUtil.init(name, enName, userName, password, path, gitBaseURL);
            settings.save();
        });
    }

    public Settings findDocSetting(String id) {
        return settingsDao.findById(id);
    }

    public boolean deleteDocSetting(String id) {
        Settings settings = findDocSetting(id);
        String enName = settings.getEnName();
        String docOrgPath = settings.getDocOrgPath();
        String contentFilePath = DocsifyConfig.defaultPath + File.separator + docOrgPath + File.separator + enName;
        return DbTxUtil.tx(() -> {
            settings.delete();
            boolean deleteContentFilePathFlag = FileUtil.deleteDir(new File(contentFilePath));
            if (!deleteContentFilePathFlag) {
                DocsifyException.throwE("文件删除异常");
            }
            boolean deleteGitFilePathFlag = FileUtil.deleteDir(new File(contentFilePath + ".git"));
            if (!deleteGitFilePathFlag) {
                DocsifyException.throwE("文件删除异常");
            }
        });
    }
}